package cn.xiaoyu.su.service.impl;

import cn.xiaoyu.su.manager.ProductManager;
import cn.xiaoyu.su.model.ProductDO;
import cn.xiaoyu.su.mapper.ProductMapper;
import cn.xiaoyu.su.service.ProductService;
import cn.xiaoyu.su.vo.ProductVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ⼆当家⼩D
 * @since 2022-12-18
 */
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductManager productManager;

    /**
     * 查看商品列表
     * @return
     */
    @Override
    public List<ProductVO> list() {

        List<ProductDO> productDOList = productManager.list();

        List<ProductVO> productVOList = productDOList.stream().map(obj -> beanProcess(obj)).collect(Collectors.toList());

        return productVOList;
    }

    /**
     * 查看商品详情
     * @param productId
     * @return
     */
    @Override
    public ProductVO findDetailById(Long productId) {

        ProductDO productDO = productManager.findDetailById(productId);

        return beanProcess(productDO);
    }

    private ProductVO beanProcess(ProductDO productDO) {
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(productDO,productVO);
        return productVO;
    }
}
